package com.lly.pro.repositories;

import com.lly.pro.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 参数检查工具
 * @author Hommond
 * @date 2/22/2019
 */
public interface UserRepository extends JpaRepository<User,Long> {
    /**
     * 用户名，密码是否正确
     * @Param userName 用户名
     * @Param password 密码
     * @return
     */
    @Query(value = "select count(u) from User u where u.userName = ?1 and u.password = ?2")
    Integer findByUserNameAndPassword(String userName,String password);

    /**
     * @Param userName 用户名
     * @return
     */
    @Query(value = "select u.userId,u.userName,u.level,u.quarters,u.department,u.birthday,u.telephone,u.email,u.address,u.picture from User u where u.userName = ?1")
    Page<User> findByUserName(String userName, Pageable pageable);

    /**
     * 根据用户名和密码获取userId
     * @Param userName 用户名
     * @Param password 密码
     * @return
     */
    @Query(value = "select u.userId from User u where u.userName = ?1 and u.password = ?2")
    List findUserId(String userName, String password);

    @Query(value = "select COUNT(u) from User u where u.userName = ?1 and u.password = ?2")
    Long findByUserIdAndPassword(long userId, String password);

}
